from functools import wraps
from flask import request, g
from app.models.user import User
from app.utils.response import error_response

def admin_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        # 这里应该根据你的认证方式来获取用户信息
        # 示例使用header中的user_id
        user_id = request.headers.get('X-User-ID')
        if not user_id:
            return error_response('未登录', 401)

        user = User.query.get(user_id)
        if not user or not user.is_admin:
            return error_response('无权限', 403)

        g.current_user = user
        return f(*args, **kwargs)
    return decorated_function